package com.d3works.apps.account.service;

import com.d3works.apps.account.db.TaxPaymentMoveLine;
import com.d3works.apps.base.service.app.AppBaseService;
import com.d3works.exception.D3worksException;
import com.d3works.inject.Beans;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class TaxPaymentMoveLineServiceImpl implements TaxPaymentMoveLineService {

  @Override
  public TaxPaymentMoveLine computeTaxAmount(TaxPaymentMoveLine taxPaymentMoveLine)
      throws D3worksException {
    BigDecimal taxRate = taxPaymentMoveLine.getTaxRate().divide(new BigDecimal(100));
    BigDecimal base = taxPaymentMoveLine.getDetailPaymentAmount();
    taxPaymentMoveLine.setTaxAmount(base.multiply(taxRate).setScale(2, RoundingMode.HALF_UP));
    return taxPaymentMoveLine;
  }

  @Override
  public TaxPaymentMoveLine getReverseTaxPaymentMoveLine(TaxPaymentMoveLine taxPaymentMoveLine)
      throws D3worksException {
    TaxPaymentMoveLine reversetaxPaymentMoveLine =
        new TaxPaymentMoveLine(
            taxPaymentMoveLine.getMoveLine(),
            taxPaymentMoveLine.getOriginTaxLine(),
            taxPaymentMoveLine.getReconcile(),
            taxPaymentMoveLine.getTaxRate(),
            taxPaymentMoveLine.getDetailPaymentAmount().negate(),
            Beans.get(AppBaseService.class)
                .getTodayDate(taxPaymentMoveLine.getReconcile().getCompany()));
    reversetaxPaymentMoveLine = this.computeTaxAmount(reversetaxPaymentMoveLine);
    reversetaxPaymentMoveLine.setIsAlreadyReverse(true);
    reversetaxPaymentMoveLine.setVatSystemSelect(taxPaymentMoveLine.getVatSystemSelect());
    taxPaymentMoveLine.setIsAlreadyReverse(true);
    return reversetaxPaymentMoveLine;
  }
}
